using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The FamilyMemberRelationshipTableDB class is responsible for interacting with the database to retrieve and store information
/// about FamilyMemberRelationshipTable objects.
/// </summary>
public class FamilyMemberRelationshipTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of FamilyMemberRelationshipTable from the underlying datasource.
/// </summary>
/// <param name="UserAccountKey">The unique RelationshipID of the FamilyMemberRelationshipTable in the database.</param>
/// <returns>An FamilyMemberRelationshipTable when the RelationshipID was found in the database, or null otherwise.</returns>
public static FamilyMemberRelationshipTable GetItem(int relationshipID)
{
FamilyMemberRelationshipTable myFamilyMemberRelationshipTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@relationshipID", relationshipID);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myFamilyMemberRelationshipTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myFamilyMemberRelationshipTable;
}
}

public static FamilyMemberRelationshipTable GetItemByUserAccountKey(int UserAccountKey)
{
    FamilyMemberRelationshipTable myFamilyMemberRelationshipTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableSelectSingleItemByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myFamilyMemberRelationshipTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myFamilyMemberRelationshipTable;
    }
}


public static FamilyMemberRelationshipTable GetItemByFamilyMemberProfileKey(int FamilyMemberProfileKey)
{
    FamilyMemberRelationshipTable myFamilyMemberRelationshipTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableSelectSingleItemByFamilyMemberProfileKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@FamilyMemberProfileKey", FamilyMemberProfileKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myFamilyMemberRelationshipTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myFamilyMemberRelationshipTable;
    }
}

/// <summary>
/// Returns a list with FamilyMemberRelationshipTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberRelationshipTable objects.</returns>
public static FamilyMemberRelationshipTableList GetList()
{
FamilyMemberRelationshipTableList tempList = new FamilyMemberRelationshipTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with FamilyMemberRelationshipTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberRelationshipTable objects.</returns>
public static FamilyMemberRelationshipTableList GetListByRelationshipTypeCode(int RelationshipTypeCode)
{
FamilyMemberRelationshipTableList tempList = new FamilyMemberRelationshipTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableSelectListByRelationshipTypeCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@relationshipTypeCode", RelationshipTypeCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a FamilyMemberRelationshipTable in the database.
/// </summary>
/// <param name="myFamilyMemberRelationshipTable">The FamilyMemberRelationshipTable instance to save.</param>
/// <returns>The new RelationshipID if the FamilyMemberRelationshipTable is new in the database or the existing RelationshipID when an item was updated.</returns>
public static int Save(FamilyMemberRelationshipTable myFamilyMemberRelationshipTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myFamilyMemberRelationshipTable.RelationshipID == -1)
{myCommand.Parameters.AddWithValue("@relationshipID", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@relationshipID", myFamilyMemberRelationshipTable.RelationshipID);
}
if (myFamilyMemberRelationshipTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myFamilyMemberRelationshipTable.UserAccountKey);
}
if (myFamilyMemberRelationshipTable.FamilyMemberProfileKey == null){
myCommand.Parameters.AddWithValue("@familyMemberProfileKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@familyMemberProfileKey", myFamilyMemberRelationshipTable.FamilyMemberProfileKey);
}
if (myFamilyMemberRelationshipTable.RelationshipTypeCode == null){
myCommand.Parameters.AddWithValue("@relationshipTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@relationshipTypeCode", myFamilyMemberRelationshipTable.RelationshipTypeCode);
}
if (myFamilyMemberRelationshipTable.EmergencyContact == null){
myCommand.Parameters.AddWithValue("@emergencyContact", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@emergencyContact", myFamilyMemberRelationshipTable.EmergencyContact);
}
if (myFamilyMemberRelationshipTable.Remark == null){
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myFamilyMemberRelationshipTable.Remark);
}
if (myFamilyMemberRelationshipTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myFamilyMemberRelationshipTable.Status);
}
if (myFamilyMemberRelationshipTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myFamilyMemberRelationshipTable.CreatedDateTime);
}
if (myFamilyMemberRelationshipTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myFamilyMemberRelationshipTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a FamilyMemberRelationshipTable from the database.
/// </summary>
/// <param name="relationshipID">The RelationshipID of the FamilyMemberRelationshipTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int relationshipID)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@relationshipID", relationshipID);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

public static bool DeleteByFamilyMemberProfileKey(int FamilyMemberProfileKey)
{
    int result = 0;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberRelationshipTableDeleteSingleItemByProfileKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;

            myCommand.Parameters.AddWithValue("@FamilyMemberProfileKey", FamilyMemberProfileKey);
            myConnection.Open();
            result = myCommand.ExecuteNonQuery();
            myConnection.Close();
        }
    }
    return result > 0;
}

public static FamilyMemberRelationshipTableList GetList(int UserAccountKey)
{
    FamilyMemberRelationshipTableList tempList = new FamilyMemberRelationshipTableList();
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocUserFamilyMemberRelationshipTableSelectList", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);
            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.HasRows)
                {
                    while (myReader.Read())
                    {
                        tempList.Add(FillUserFamilyRelationshipList(myReader));
                    }
                }
                myReader.Close();
            }
        }
    }
    return tempList;
}



#endregion

/// <summary>
/// Initializes a new instance of the FamilyMemberRelationshipTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static FamilyMemberRelationshipTable FillDataRecord(IDataRecord myDataRecord )
{
FamilyMemberRelationshipTable myFamilyMemberRelationshipTable = new FamilyMemberRelationshipTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipID")))
{
myFamilyMemberRelationshipTable.RelationshipID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("RelationshipID"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myFamilyMemberRelationshipTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberProfileKey")))
{
myFamilyMemberRelationshipTable.FamilyMemberProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberProfileKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipTypeCode")))
{
myFamilyMemberRelationshipTable.RelationshipTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RelationshipTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmergencyContact")))
{
myFamilyMemberRelationshipTable.EmergencyContact = myDataRecord.GetString(myDataRecord.GetOrdinal("EmergencyContact"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myFamilyMemberRelationshipTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myFamilyMemberRelationshipTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myFamilyMemberRelationshipTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myFamilyMemberRelationshipTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myFamilyMemberRelationshipTable;
}

private static FamilyMemberRelationshipTable FillUserFamilyRelationshipList(IDataRecord myDataRecord)
{
    FamilyMemberRelationshipTable myFamilyMemberRelationshipTable = new FamilyMemberRelationshipTable();
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipID")))
    {
        myFamilyMemberRelationshipTable.RelationshipID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("RelationshipID"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
    {
        myFamilyMemberRelationshipTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberProfileKey")))
    {
        myFamilyMemberRelationshipTable.FamilyMemberProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberProfileKey"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberUserAccountKey")))
    {
        myFamilyMemberRelationshipTable.FamilyMemberUserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberUserAccountKey"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FullName")))
    {
        myFamilyMemberRelationshipTable.FullName = myDataRecord.GetString(myDataRecord.GetOrdinal("FullName"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherName")))
    {
        myFamilyMemberRelationshipTable.OtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherName"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipTypeCode")))
    {
        myFamilyMemberRelationshipTable.RelationshipTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RelationshipTypeCode"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipDesc")))
    {
        myFamilyMemberRelationshipTable.RelationshipDesc = myDataRecord.GetString(myDataRecord.GetOrdinal("RelationshipDesc"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmergencyContact")))
    {
        myFamilyMemberRelationshipTable.EmergencyContact = myDataRecord.GetString(myDataRecord.GetOrdinal("EmergencyContact"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
    {
        myFamilyMemberRelationshipTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
    {
        myFamilyMemberRelationshipTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
    {
        myFamilyMemberRelationshipTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
    }
    if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
    {
        myFamilyMemberRelationshipTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
    }
    return myFamilyMemberRelationshipTable;
}
}
